// pages/dachu/dachu.js
const API = require('../../http/API')
const http = require('../../http/http')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    title: [
      { id: 0, text: "大触讲座" },
      { id: 1, text: "大触专访" },
      { id: 2, text: "热门大触" },
    ],
    currentIndex: 0,
    page: 1,
    left: [],
    center: [],
    right: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getLeft()
  },
  // 点击获取索引
  clickTap(e) {
    this.setData({ currentIndex: e.currentTarget.dataset.index })
    this.setData({ page: 1 })
    if (e.currentTarget.dataset.index === 0) {
      this.setData({ page: this.data.page + 1, })
      this.getLeft()
    } else if (e.currentTarget.dataset.index === 1) {
      this.getCenter()
    } else if (e.currentTarget.dataset.index === 2) {
      this.getRight()
    }
  },
  getLeft() {
    http(API.dachu, {
      link: "paging",
      live: 3,
      heat: "N",
      page: this.data.page,
    }).then(res => {
      // console.log(res.data.course.data);
      if (this.data.page === 1) {
        this.setData({ left: res.data.course.data })
      } else {
        this.setData({ left: this.data.left.concat(res.data.course.data) })
      }
    })
  },
  getCenter() {
    http(API.dachuInterview, {
      link: "paging",
      type: "interview",
      page: this.data.page,
    }).then(res => {
      if (this.data.page === 1) {
        this.setData({ center: res.data.data })
      } else {
        this.setData({ center: this.data.center.concat(res.data.data) })
      }
    })
  },
  getRight() {
    http(API.dachuLists, {
      link: "paging",
      page: this.data.page,
    }).then(res => {
      if (this.data.page === 1) {
        this.setData({ right: res.data.data })
      } else {
        this.setData({ right: this.data.right.concat(res.data.data) })
      }
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.currentIndex === 0) {
      this.setData({ page: this.data.page + 1, })
      this.getLeft()
    } else if (this.data.currentIndex === 1) {
      this.setData({ page: this.data.page + 1, })
      this.getCenter()
    } else if (this.data.currentIndex === 2) {
      this.setData({ page: this.data.page + 1, })
      this.getRight()
    }
  }
})